package com.sanpu.kxe.business.main.presenter;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.meiqia.meiqiasdk.util.MQIntentBuilder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.AboutContract;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.module.dialog.CallServiceDialog;
import com.sanpu.kxe.module.dialog.WechatServiceDialog;
import com.sanpu.kxe.utils.IntentTool;

import java.util.HashMap;

/**
 * Description：在线咨询Presenter
 * Created by 薛勇军 on 2018/7/12.
 */

public class AboutPresenter implements AboutContract.Presenter {

    private AboutContract.View mView;

    private WechatServiceDialog wechatDialog = null;

    private CallServiceDialog dialog = null;

    private String merchantCode, mName, mTelPhone;

    public AboutPresenter(AboutContract.View mView) {
        this.mView = mView;
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            merchantCode = centerOutput.getMerchantCode();
            if (!TextUtils.isEmpty(centerOutput.getCrpNm())) {
                mName = centerOutput.getCrpNm() + "(" + centerOutput.getUserName() + ")";
            } else {
                mName = centerOutput.getUserName();
            }
            mTelPhone = centerOutput.getUserName();
        }
    }

    @Override
    public void callSerivce(String serviceNumber) {
        if (dialog == null) {
            dialog = new CallServiceDialog(mView.getContext())
                    .title("客服热线")
                    .content(serviceNumber);
        }
        dialog.show();
        final String substring = serviceNumber.replaceAll("-", "");
        dialog.setCallLinstener(new CallServiceDialog.OnCallLinstener() {
            @Override
            public void OnClickListener() {
                Uri uri = Uri.parse("tel:" + substring);
                Intent it = new Intent(Intent.ACTION_DIAL, uri);
                mView.getContext().startActivity(it);
            }
        });
    }

    @Override
    public void tencentSerivce(String tencent) {
        if (wechatDialog == null) {
            wechatDialog = new WechatServiceDialog(mView.getContext(), tencent)
                    .title("提示")
                    .content(CommonUtils.generateHintView(mView.getContext(), R.drawable.ic_qrcode_tencent,
                            "已帮您复制微信服务号：请打开微信->右上角“+”号->添加朋友->公众号->粘贴->搜索"));
        }
        wechatDialog.show();
    }

    @Override
    public void irongbeiSerivce(String irongbei) {
        if (wechatDialog == null) {
            wechatDialog = new WechatServiceDialog(mView.getContext(), irongbei)
                    .title("提示")
                    .content(CommonUtils.generateHintView(mView.getContext(), R.drawable.ic_qrcode_irongbei,
                            "已帮您复制微信服务号：请打开微信->右上角“+”号->添加朋友->公众号->粘贴->搜索"));
        }
        wechatDialog.show();
    }

    @Override
    public void conversationWrapper() {
        if (ContextCompat.checkSelfPermission(mView.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) mView.getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    CommonConfig.WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        } else {
            HashMap<String, String> clientInfo = new HashMap<>();
            clientInfo.put("name", mName);
            clientInfo.put("tel", mTelPhone);
            HashMap<String, String> updateInfo = new HashMap<>();
            updateInfo.put("name", mName);
            Intent intent = new MQIntentBuilder(mView.getContext())
                    .setClientInfo(clientInfo)
                    .updateClientInfo(updateInfo)
                    .setCustomizedId(merchantCode)
                    .build();
            IntentTool.startActivity(mView.getContext(), intent);
        }
    }


    @Override
    public void onDestroyPresenter() {
        if (mView != null) {
            mView = null;
        }
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
        if (wechatDialog != null && wechatDialog.isShowing()) {
            wechatDialog.dismiss();
            wechatDialog = null;
        }
    }
}
